package com.hzncc.scaffold.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Other.Training;
import com.hzncc.scaffold.entity.Vo.AttachFileVo;
import com.hzncc.scaffold.mapper.TrainingMapper;
import com.hzncc.scaffold.service.TrainingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 实训表 服务实现类
 * </p>
 *
 * @author DrunkBear
 * @since 2022-04-27
 */
@Service
public class TrainingServiceImpl extends ServiceImpl<TrainingMapper, Training> implements TrainingService {

    @Autowired
    TrainingService trainingService;

    @Override
    public ResultT update(Training training) {
        boolean update = trainingService.updateById(training);
        if (update) {
            return ResultT.success("修改成功");
        } else {
            return ResultT.fail("修改失败");
        }
    }

    @Override
    public ResultT stayAtTop(Integer trainingId, Integer stayAtTop) {
        if (trainingService.getById(trainingId) != null) {
            Training training = trainingService.getById(trainingId);
            if (stayAtTop == 0 || stayAtTop == 1) {
                training.setStayAtTop(stayAtTop);
                training.setSortTime(DateUtil.now());
            } else {
                return ResultT.fail("数据错误");
            }
            boolean update = trainingService.updateById(training);
            if (update) {
                if (stayAtTop == 1) {
                    return ResultT.success("置顶成功");
                } else {
                    return ResultT.success("取消置顶成功");
                }

            } else {
                return ResultT.fail("置顶失败");
            }
        } else {
            return ResultT.fail("数据错误");
        }
    }

    @Override
    public ResultT detail(Integer trainingId) {
        if (trainingService.getById(trainingId) != null) {
            Training training = trainingService.getById(trainingId);

            JSONArray jsonArray = new JSONArray(training.getAttachFileStr());
            List<AttachFileVo> attachFileVoList = jsonArray.toList(AttachFileVo.class);
            training.setAttachFile(attachFileVoList);

            return ResultT.success(training);
        } else {
            return ResultT.fail("数据错误");
        }
    }
}
